import browser from "webextension-polyfill";

const getOptionsUrl = () => `chrome-extension://${browser.runtime.id}/src/entries/options/index.html`

const getOptionTab = async () => {
  const tas = await chrome.tabs.query({})
  return tas.find(tab => tab.url === getOptionsUrl())
}

export const openOptionsPage = async () => {
  const createData:browser.Tabs.CreateCreatePropertiesType = {
    url: getOptionsUrl()
  }

  const openedTap = await getOptionTab()

  if(!openedTap) return browser.tabs.create(createData)

  browser.windows.update(openedTap.windowId as number, {focused: true}); // 更新窗口
  browser.tabs.update(openedTap.id, {active: true});  // 更新tab
  
}

export const usePopup = () => {

  return {
    openOptionsPage
  }
}